package com.yex.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.base.WalletOper;
import com.yex.base.WalletOperEnum;
import com.yex.base.ZeroResultException;
import com.yex.common.utils.PageUtils;
import com.yex.dao.IntegralDao;
import com.yex.entity.IntegralEntity;
import com.yex.entity.IntegralLogEntity;
import com.yex.service.IntegralLogService;
import com.yex.service.IntegralService;
import com.yex.service.LocaleMessageSourceService;
import com.yex.utils.JsonResultVo;
import com.yex.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


@Service("integralService")
public class IntegralServiceImpl extends ServiceImpl<IntegralDao, IntegralEntity> implements IntegralService {
    @Autowired
    private LocaleMessageSourceService message;
    @Autowired
    private IntegralLogService integralLogService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<IntegralEntity> page = this.selectPage(
                new Query<IntegralEntity>(params).getPage(),
                new EntityWrapper<IntegralEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public IntegralEntity selectByUserId(Integer userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    @Transactional
    public JsonResultVo updateIntegralt(WalletOper walletOper) {
        IntegralEntity integralEntity = new IntegralEntity();
        integralEntity.setUserId(walletOper.getUserId());
        if (walletOper.getId() == null && WalletOperEnum.ADD == walletOper.getOper()) {
            integralEntity.setTotal(walletOper.getAmount());
            int result = baseMapper.insertWallet(integralEntity);
            if (result <= 0) {
                throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
            }

            IntegralEntity integral = baseMapper.selectByUserId(walletOper.getUserId());

            IntegralLogEntity integralLogEntity = new IntegralLogEntity();
            integralLogEntity.setUserId(walletOper.getUserId());
            integralLogEntity.setStatus(walletOper.getOper().ordinal());
            integralLogEntity.setRemark(walletOper.getRemark());
            integralLogEntity.setTotal(walletOper.getAmount());
            integralLogEntity.setWalletId(integral.getId());
            integralLogEntity.setCreateTime(new Date());
            if (!integralLogService.insert(integralLogEntity)) {
                throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
            }

            return result > 0 ? new JsonResultVo(message.getMessage("success"), 0) : new JsonResultVo(message.getMessage("fail"), -1);
        }

        integralEntity.setId(walletOper.getId());

        if (walletOper.getAmount().compareTo(BigDecimal.ZERO) < 0) {
            throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
        }

        int result = -1;
        if (WalletOperEnum.FROZEN == walletOper.getOper()) {
            integralEntity.setFrozen(walletOper.getAmount());
            result = baseMapper.updateFrozen(integralEntity);
        } else if (WalletOperEnum.UNFRONZEN == walletOper.getOper()) {
            integralEntity.setFrozen(walletOper.getAmount());
            result = baseMapper.updateUnFrozen(integralEntity);
        } else if (WalletOperEnum.ADD == walletOper.getOper()) {
            integralEntity.setTotal(walletOper.getAmount());
            result = baseMapper.updateAdd(integralEntity);
        } else if (WalletOperEnum.REDUCE == walletOper.getOper()) {
            integralEntity.setTotal(walletOper.getAmount());
            result = baseMapper.updateReduce(integralEntity);
        } else if (WalletOperEnum.REDUCE_FROZEN == walletOper.getOper()) {
            integralEntity.setFrozen(walletOper.getAmount());
            result = baseMapper.updateReduceFrozen(integralEntity);
        }

        if (result <= 0) {
            throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
        }

        IntegralLogEntity integralLogEntity = new IntegralLogEntity();
        integralLogEntity.setUserId(walletOper.getUserId());
        integralLogEntity.setStatus(walletOper.getOper().ordinal());
        integralLogEntity.setRemark(walletOper.getRemark());
        integralLogEntity.setTotal(walletOper.getAmount());
        integralLogEntity.setWalletId(walletOper.getId());
        integralLogEntity.setCreateTime(new Date());

        if (!integralLogService.insert(integralLogEntity)) {
            throw new ZeroResultException(new JsonResultVo(message.getMessage("error_info_zero")));
        }

        return new JsonResultVo(message.getMessage("success"));
    }

}
